﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Layout;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class BenzerRenkler4 : XForm
    {
        //TODO: Cat headerpanel'de gözüksün (benzerrenkler3'teki gibi)
        int ProductId;
        public BenzerRenkler4(int _ProductId) :
            this()
        {
            ProductId = _ProductId;
            gle.EditValue = ProductId;
        }

        public BenzerRenkler4()
        {
            InitializeComponent();
        }

        string[] cats = @"1. Düz;2. Küçük kareli;3. Büyük Kareli;4. Kalın Çizgili;5. İnce Çizgi;6. Baskılı".Split(";".ToCharArray());

        private void BenzerRenkler4_Load(object sender, EventArgs e)
        {
            Notify("Kumaş resimlerine Ctrl+Click: zoom, Shift+Click: yeni bir arama ile tıklanan kumaşın benzerlerini getirir.");

            gle.Edit.Assign(RI(Rp.V_Kumas));
            gle.EditValueChanged += gle_EditValueChanged;
            InitLV();

            if (gle.EditValue != null)
                GetSimilars();

            //cbMode.CheckedChanged += cbMode_CheckedChanged;

        }

        void gle_EditValueChanged(object sender, EventArgs e)
        {
            GetSimilars();
        }


        void cbMode_CheckedChanged(object sender, EventArgs e)
        {
            GetSimilars();
        }

        string ProductIds;
        private void GetSimilars()
        {
            ProductId = gle.EditValue.ToInt();

            //int Adet = cbMode.Checked ? 150 : 36;
            int Adet = 150;
            List<int> result = GetSimilarsTopN(ProductId, Adet);

            ProductIds = "";
            if (result.Count > 0)
            {
                foreach (int i in result)
                    ProductIds += i.ToString() + ",";

                gcRenkler.DataSource = RenewData();

                lblKategori.Caption = string.Empty;
                if (((DataTable)gcRenkler.DataSource).Rows.Count > 0)
                    lblKategori.Caption = "Kategori: " + cats[intParse(((DataTable)gcRenkler.DataSource).Rows[0]["Cat"]) - 1];
            }
            else
                ShowInfoMessage("Benzerlik hesaplanamadı!");
        }


        LayoutViewField lvfPhoto;
        int sz = 150;
        Color color1 = Color.Red;
        Color color2 = Color.Transparent;

        void InitLV()
        {

            //lvRenkler.TemplateCard.MaxSize = new Size(x, ilkAyrim);
            lvRenkler.Click -= new EventHandler(lvRenkler_Click);
            //lvRenkler.CustomDrawCardFieldValue -= new global::DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(lvRenkler_CustomDrawCardFieldValue);
            lvRenkler.Columns.Clear();

            lvRenkler.GridControl.DataSource = RenewData();
            //lvRenkler.Columns[1].DisplayFormat = "{0:StokKodu:n}";

            lvRenkler.PopulateColumns();
            lvRenkler.OptionsBehavior.Editable = false;


            lvRenkler.Click += new EventHandler(lvRenkler_Click);
            lvRenkler.Columns["F"].Caption = "SCN";
            lvRenkler.Columns["F"].ColumnEdit = new RepositoryItemPictureEdit();
            lvfPhoto = lvRenkler.Columns["F"].LayoutViewField;
            lvfPhoto.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            lvfPhoto.MinSize = lvfPhoto.MaxSize = new Size(sz, sz);  //140x140 idi

            lvRenkler.CardMinSize = new Size(sz, sz);
            lvRenkler.OptionsMultiRecordMode.MultiRowScrollBarOrientation = DevExpress.XtraGrid.Views.Layout.ScrollBarOrientation.Vertical;
            lvRenkler.OptionsView.ViewMode = DevExpress.XtraGrid.Views.Layout.LayoutViewMode.MultiRow;
            lvRenkler.OptionsView.ShowCardCaption = false;
            lvRenkler.OptionsView.ShowCardExpandButton = false;
            lvRenkler.OptionsBehavior.UseTabKey = true;
            lvRenkler.OptionsBehavior.KeepFocusedRowOnUpdate = true;

            //lvRenkler.OptionsItemText.TextToControlDistance = 10;
            //lvRenkler.OptionsMultiRecordMode.StretchCardToViewWidth = true;
            //lvRenkler.OptionsSelection.MultiSelect = true;
            //lvRenkler.OptionsView.ContentAlignment = System.Drawing.ContentAlignment.TopCenter;
            //lvRenkler.OptionsView.ShowCardBorderIfCaptionHidden = false;
            //lvRenkler.OptionsView.ShowCardLines = false;
            //lvRenkler.OptionsView.ShowFieldHints = false;
            //lvRenkler.OptionsView.ShowHeaderPanel = false;

            lvRenkler.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;

            foreach (LayoutViewField lvf in lvRenkler.TemplateCard.Items)
            {
                lvf.Padding = new DevExpress.XtraLayout.Utils.Padding(4, 4, 0, 0);
                lvf.EditorPreferredWidth = sz;
            }
            layoutViewCard1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 0, 0);

            //foreach (object ci in lvRenkler.Items)
            //    if (ci is LayoutControlItem)
            //        ((LayoutControlItem)ci).TextVisible = false;


            //lvRenkler.Columns["QtyIDLE"].Caption = "IDLE";
            //lvRenkler.Columns["QtyAyrilan"].Caption = "Ayrılan";
            //lvRenkler.Columns["StokKodu"].Caption = "Art";
            lvRenkler.Columns["Id"].Caption = "PId";
            lvRenkler.Columns["Cat"].Visible = false;
            lvRenkler.Columns["idx1"].Visible = false;

            foreach (LayoutViewColumn lvc in lvRenkler.Columns)
            {
                lvc.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
                lvc.OptionsFilter.AllowFilter = false;
            }

        }

        private DataTable RenewData()
        {
            return Session.DataManager.ExecuteDataTable("GetBenzer4", ProductIds);
        }


        void lvRenkler_Click(object sender, EventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control)
            {
                DataRow dr = lvRenkler.GetFocusedDataRow();
                Goster(intParse(dr["Id"]));
            }
            else if (Control.ModifierKeys == Keys.Shift)
            {
                DataRow dr = lvRenkler.GetFocusedDataRow();
                gle.EditValue = intParse(dr["Id"]);
                GetSimilars();
            }
        }

    }
}
